    //
//  HomeViewController.m
//  coocsless
//
//  Created by fabian on 04.03.11.
//  Copyright 2011 roboboogie.us. All rights reserved.
//

#import "HomeViewController.h"
#import "ContainerViewController.h"
#import "Global.h"



@implementation HomeViewController

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


-(void) viewWillAppear:(BOOL)animated {
	// !Achtung! // toDo: NUR !!! NUR  wenn sich die Sprache zum letzten Mal geändert loadView ausführen!
	[self loadView];
	
	
}

- (void)loadView {
    [super loadView];
	
	[[self view] setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg2.png"]]];
	
	buttonScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 680, [[self view] bounds].size.width , 88)];
	[buttonScrollView setBackgroundColor:[UIColor colorWithRed:0.078f green:0.078f blue:0.078f alpha:1.0f]];
	[buttonScrollView setClipsToBounds:YES];

	if([Global sharedGlobal].mode==0) {
		
		NSArray *decadesNames = [[Global sharedGlobal] getDecadeTranslations ];
		int scrollerWidth = 0;
		int j = 10;
		for (int i = 0 ; i<[decadesNames count]; i++) {
			CGRect frame = CGRectMake(j, 10, 200, 40);
			HomeButtonView *button = [[HomeButtonView alloc] initWithFrame:frame andTitle:[NSString stringWithFormat:@"%@",[decadesNames objectAtIndex:i]] andButtonFunction:@"gotoDecadePage" ];
			[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
			[button setScrollViewPage:i+1];
			[button setDelegate:self];
			[button sizeToFit];
			[buttonScrollView addSubview:button];
			int buttonWidth = [[button titleLabel] bounds].size.width;
			j = j + buttonWidth+40;
			scrollerWidth = scrollerWidth+buttonWidth+40; 
			NSLog(@"%d",scrollerWidth);
			[button release];
		}
		[buttonScrollView setContentSize:CGSizeMake(scrollerWidth,88)];
		[[self view] addSubview:buttonScrollView];
		
	} else if ([Global sharedGlobal].mode==1){
		
		NSArray *categoryNames = [[Global sharedGlobal] getCategoriesTranslations];
		int scrollerWidth = 0;
		int j = 10;
		for (int i = 0 ; i<[categoryNames count]; i++) {
			
			CGRect frame = CGRectMake(j, 20, 200, 40);
			HomeButtonView *button = [[HomeButtonView alloc] initWithFrame:frame andTitle:[NSString stringWithFormat:@"%@",[categoryNames objectAtIndex:i]] andButtonFunction:@"gotoDecadePage" ];
			[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
			[button setScrollViewPage:i+1];
			[button sizeToFit];
			[button setDelegate:self];
			//[button setFont:[UIFont fontWithName:@"Archer" size:34]];
			[buttonScrollView addSubview:button];
			int buttonWidth = [[button titleLabel] bounds].size.width;
			j = j + buttonWidth+40;
			scrollerWidth = scrollerWidth+buttonWidth+40; 
			[button release];
			
		}
		[buttonScrollView setContentSize:CGSizeMake(scrollerWidth,88)];
		[[self view] addSubview:buttonScrollView];
		
	}

	
	
	// merke: height und width sind getauscht!
	float scrollViewWidth  = 1024;
	
	NSLog(@"scrolViewWidth: %f",scrollViewWidth);
	decadesScrollView =  [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth, 600)];
	
	[decadesScrollView setCanCancelContentTouches:NO];
	[decadesScrollView setClipsToBounds:YES];
	[decadesScrollView setPagingEnabled:YES];
	[decadesScrollView setDelegate:self];
	
	
	/////////////* langweilige normale Bilder */
	UIImageView *moneyboy = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1950homescreen.png"]];
	CGRect frame = CGRectMake(100, 100, 897, 575);
	[moneyboy setFrame:frame];
	UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(900, 0, 90, 30)];
	[label1 setBackgroundColor:[UIColor clearColor]];
	[label1 setTextColor:[UIColor redColor]];
	[label1 setFont:[UIFont fontWithName:@"Archer" size:40]];
	[label1 setText:@"1910"];

	[decadesScrollView addSubview:label1];	
	[decadesScrollView addSubview:moneyboy];
	
	UIImageView *moneyboy1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1950homescreen.png"]];
	CGRect frame2 = CGRectMake(100+scrollViewWidth, 100, 897, 575);
	[moneyboy1 setFrame:frame2];
	UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(900+scrollViewWidth, 0, 90, 30)];
	[label2 setBackgroundColor:[UIColor clearColor]];
	[label2 setTextColor:[UIColor redColor]];
	[label2 setFont:[UIFont fontWithName:@"Archer" size:40]];
	[label2 setText:@"1920"];
	
	[decadesScrollView addSubview:label2];
	[decadesScrollView addSubview:moneyboy1];

	
	//////////////* Hier ein mal mit dem ThumbImageView ... */
	
	UIImage *moneyboy2 = [UIImage imageNamed:@"1950homescreen.png"];
	CGRect frame3 = CGRectMake((scrollViewWidth*2)+100, 100, 897, 575);
	
	ThumbImageView *thumbView = [[ThumbImageView alloc] initWithImage:moneyboy2];
	[thumbView setDelegate:self];
	[thumbView setImageName:@"article1"];

	[thumbView setFrame:frame3];
	[thumbView setHome:frame3];
	[decadesScrollView addSubview:thumbView];
	[thumbView release];
	
	UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(900+(scrollViewWidth*2), 0, 90, 30)];
	[label3 setBackgroundColor:[UIColor clearColor]];
	[label3 setTextColor:[UIColor redColor]];
	[label3 setFont:[UIFont fontWithName:@"Archer" size:40]];
	[label3 setText:@"1930"];
	
	[decadesScrollView addSubview:label3];
	[decadesScrollView setContentSize:CGSizeMake(scrollViewWidth*3,600)];
	[[self view] addSubview:decadesScrollView];
	
	
	// Zufällige-Auswahl eines Artikels llhlkh
	[self changePage:[Global sharedGlobal].currentPage];
	
	
	int i = 0;
	for(HomeButtonView *button in [buttonScrollView subviews]) {
		if([button isMemberOfClass:[HomeButtonView class]]) {
			if([Global sharedGlobal].currentPage==i) {
				[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
			} else {
				[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
			}
			i++;
		}
	}
	
}


-(void) changePage:(int)pId {
	CGRect frame = decadesScrollView.frame;
	frame.origin.x = frame.size.width * (pId);
	frame.origin.y = 0;
	[decadesScrollView scrollRectToVisible:frame animated:YES];
}

#pragma mark HomeButtonViewDelegate methods

-(void) homeButtonViewWasTapped:(HomeButtonView *)hbv {
	for(HomeButtonView *button in [buttonScrollView subviews]) {
		if([button isMemberOfClass:[HomeButtonView class]])
			[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];		
	}
	[hbv setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[self changePage:hbv.scrollViewPage-1];
}


#pragma mark ThumbImageViewDelegate methods

- (void)thumbImageViewWasTapped:(ThumbImageView *)tiv {
	ContainerViewController *articleViewController = [[ContainerViewController alloc] init];
	[self.navigationController pushViewController:articleViewController animated:NO];
}

#pragma mark UIScrollViewDelegate methods

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
	int page = decadesScrollView.contentOffset.x/1000;
	int i = 0;
	for(HomeButtonView *button in [buttonScrollView subviews]) {
		if([button isMemberOfClass:[HomeButtonView class]]) {
			if(page==i) {
				[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
			} else {
				[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
			}
			i++;
		}
	}
	
	
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
